//
//  LLYThirdViewController.m
//  ZZ15091031LiuLeiYan
//
//  Created by qianfeng001 on 15/8/28.
//  Copyright (c) 2015年 刘雷岩. All rights reserved.
//

#import "LLYThirdViewController.h"

#define randomColor arc4random()%256/255.0

@interface LLYThirdViewController ()
{
    UIBarButtonItem *_rightBarButtonItem;
    NSMutableArray *_VCArr;
    
    CGPoint _tmpCenter1;
    CGPoint _tmpCenter3;
}

@end

@implementation LLYThirdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:randomColor green:randomColor blue:randomColor alpha:1.0];
    _VCArr = [NSMutableArray array];
    [_VCArr addObject:self];
    [self createItem];
    [self createViewController];
    

}

- (void)createItem {
    _rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage:)];
    self.navigationItem.rightBarButtonItem = _rightBarButtonItem;
    self.navigationItem.rightBarButtonItem.tag = 10;
}

- (void)createViewController {
    
    for (int i=0; i<3; i++) {
        UIViewController *viewController = [[UIViewController alloc] init];
        viewController.view.backgroundColor = [UIColor colorWithRed:randomColor green:randomColor blue:randomColor alpha:1.0];
        if (i != 2) {
            viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage:)];
            viewController.navigationItem.rightBarButtonItem.tag = 11+i;
        }
        [_VCArr addObject:viewController];
    }
}


- (void)buttonClick:(UIButton *)button {
    
    [UIView animateWithDuration:1 animations:^{
        button.transform = CGAffineTransformMakeScale(2, 2);
        button.alpha = 0;
    } completion:^(BOOL finished) {
        if (button.tag == 101) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else if (button.tag == 102) {
            [self.navigationController popToViewController:_VCArr[1] animated:YES];
        }else if (button.tag == 103) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        NSArray *btnArr = [button superview].subviews;
        for (UIButton *btn in btnArr) {
            [btn removeFromSuperview];
        }
    }];
    
    
}


- (void)nextPage:(UIBarButtonItem *)item {
    NSInteger n = item.tag - 10;
//    NSLog(@"%ld",n);
    if (n == 2) {
        UIViewController *fourthVC = _VCArr[3];
        
        for (int i=0; i<3; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor blackColor];
            button.frame = CGRectMake(140, 300, 100, 100);
            button.layer.cornerRadius = 50;
            button.layer.borderColor = [UIColor whiteColor].CGColor;
            button.layer.borderWidth = 5;
            button.titleLabel.font = [UIFont systemFontOfSize:40];
            [button setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.tag = 101 +i;
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
           
            [fourthVC.view addSubview:button];
        }
        UIButton *btn1 = (UIButton *)[fourthVC.view viewWithTag:101];
        UIButton *btn3 = (UIButton *)[fourthVC.view viewWithTag:103];
        _tmpCenter1 = btn1.center;
        _tmpCenter3 = btn3.center;
        
        
        
        [UIView animateWithDuration:1 delay:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            CGPoint newCenter1 = CGPointMake(btn1.center.x, btn1.center.y - 120);
            CGPoint newCenter2 = CGPointMake(btn3.center.x, btn3.center.y + 120);
            btn1.center = newCenter1;
            btn3.center = newCenter2;
        } completion:^(BOOL finished) {
            
        }];
    }
    
    
    [self.navigationController pushViewController:_VCArr[n+1] animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
